/**
 * 寻找 from 后面的 表名
 * @param editor
 * @returns
 */ 
function getTableBySql(editor) {
    var cur = editor.getCursor();
    
    // 在当前行寻找表名
    var line = editor.getLineTokens(cur.line);
    var tableName = getTableBySqlLine(line);
    
    if (tableName == "") {
    	// 在后面的行寻找表名
    	for (var i = cur.line + 1; i < editor.lineCount(); i++){
    	    var line = editor.getLineTokens(i);
    	    var line2 = [];
    	    var end = false;
    	    for(var j = 0; j<line.length; j++){
    	    	line2.push(line[j])
    	    	var str = line[j].string.toLowerCase();
    	    	if (str.endsWith(";") || str == "union" || str == "select") {
    	    		// 发现换行标记则不再寻找
    	    		end = true;
    	    		break;
    	    	}
    	    }
    	    tableName = getTableBySqlLine(line2);
    	    if (end || tableName != "") {
	    		break;
    	    }
    	}
    }
    
    if (tableName == "") {
    	// 在前面的行寻找表名
    	for (var i = cur.line - 1; i >= 0; i--){
    	    var line = editor.getLineTokens(i);
    	    var line2 = [];
    	    var end = false;
    	    for(var j = 0; j<line.length; j++){
    	    	line2.push(line[j])
    	    	var str = line[j].string.toLowerCase();
    	    	if (str.endsWith(";") || str == "union" || str == "select") {
    	    		// 发现换行标记则清空重新寻找
    	    		end = true;
    	    		line2 = [];
    	    	}
    	    }
    	    tableName = getTableBySqlLine(line2);
    	    if (end || tableName != "") {
	    		break;
    	    }
    	}
    }
    return tableName;
 }
/**
 * 从当前的行中寻找 from 后面的 表名 TODO 暂时不支持识别 join
 * @param editor
 * @returns
 */ 
function getTableBySqlLine(line) {
	var tableName = "";
    var hasFrom = false
    if (line && line.length > 1) {
        for (var i=0; i< line.length; i++) {
        	if (line[i].string.toLowerCase() == "from") {
        		hasFrom = true;
        	} else if (hasFrom && line[i].string.trim() != '') {
        		// from aaa 时 返回 aaa
        		tableName = line[i].string;
        		if (i+2 <= line.length && line[i+1].string == "." && line[i+2].string != "") {
        		    // from aaa.bbb 时 返回 bbb
        		    tableName = line[i+2].string;
        		}
        		break;
        	}
        }
    }
    return tableName;
}

/**
 * 使用#切分并返回第一部分
 * @param field
 * @returns
 */
 function getTableFields(field) {
	var indexOf = field.indexOf("#");
    return indexOf > 0 ? field.substring(0, indexOf) : field;
 }

 /**
  * 使用#切分并返回第二部分
  * @param field
  * @returns
  */
 function getTableFieldsDesc(field) {
	var indexOf = field.indexOf("#");
    return indexOf > 0 ? field.substring(indexOf + 1, field.length) : "";
 }

 /**
  * 如果 firstKey 存在于 keys, 则将其放入 keys 的第一位
  */
 function reorderKey(keys, firstKey) {
    if (!firstKey) {
        return keys;
    }
    if (isArray(keys)){
        var res = [];
        for (var k in keys){
            var field = getTableFields(keys[k]);
            if (field.toLowerCase() == firstKey.toLowerCase()) {
                res.push(keys[k]);
            }
        }
        for (var k in keys){
            var field = getTableFields(keys[k]);
            if (field.toLowerCase() != firstKey.toLowerCase()) {
                res.push(keys[k]);
            }
        }
        return res;
	} else {
	    var res = {};
        for (var k in keys){
            var field = getTableFields(k);
            if (field.toLowerCase() == firstKey.toLowerCase()) {
                res[k] = keys[k];
            }
        }
        for (var k in keys){
            var field = getTableFields(k);
            if (field.toLowerCase() != firstKey.toLowerCase()) {
                res[k] = keys[k];
            }
        }
        return res;
	}
 }


 /**
  * 如果 firstKey 存在于 keys, 则将其放入 keys 的第一位
  */
 function getSingleKey(keys, firstKey) {
    if (!firstKey) {
        return keys;
    }
    if (isArray(keys)){
        var res = [];
        for (var k in keys){
            var field = getTableFields(k);
            if (field.toLowerCase() == firstKey.toLowerCase()) {
                res.push(field);
            }
        }
        return res;
	} else {
	    var res = {};
        for (var k in keys){
            var field = getTableFields(k);
            if (field.toLowerCase() == firstKey.toLowerCase()) {
                res[k] = keys[k];
            }
        }
        return res;
	}
 }

 function isArray(val) { return Object.prototype.toString.call(val) == "[object Array]" }
 
 
function isTableName(stream) {
	var tmp = stream.string.substr(0, stream.start).replace(/\s+$/g, ' ').replace(/\s/g, ' ').toLowerCase();
	return tmp.endWiths(" from ") || tmp.endWiths(" join ");
}